package com.dogsong.liteflow.editor.controller;

import com.dogsong.liteflow.editor.bean.*;
import com.dogsong.liteflow.editor.service.BizService;
import com.google.common.collect.Lists;
import com.yomahub.liteflow.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * BizController
 *
 * @author <a href="mailto:dogsong99@163.com">dosong</a>
 * @since 2024/6/12
 */
@Api(tags = "基础业务接口")
@RequestMapping("/api")
@RestController
@Slf4j
public class BizController {

    @Autowired
    private BizService bizService;

    @ApiOperation(value = "获取LF组件", notes = "输入空查全部")
    @GetMapping("/getCmpList")
    @ResponseBody
    public List<CmpInfoVO> getCmpList() {
        log.info("getCmpList 获取LF组件");
        List<CmpInfoVO> cmpList = Lists.newArrayList();
        try {
            cmpList = bizService.getCmpList();
        } catch (Exception ex) {
            log.error("-----", ex);
        }
        log.info("getCmpList 获取LF组件完成,共[{}]条", cmpList.size());
        return cmpList;
    }

    @ApiOperation(value = "获取LF链", notes = "输入空查全部")
    @GetMapping("/getChainList")
    @ResponseBody
    public List<ChainInfoVO> getChainList() {
        log.info("getChainList 获取LF链");
        List<ChainInfoVO> chainList = Lists.newArrayList();
        try {
            chainList = bizService.getChainList();
        } catch (Exception ex) {
            log.error("-----", ex);
        }
        log.info("getChainList 获取LF链完成,共[{}]条", chainList.size());
        return chainList;
    }

    @ApiOperation(value = "校验EL表达式", notes = "组装表达式时会对表达式的参数类型进行校验")
    @ApiImplicitParam(name = "jsonEl", value = "json结构", required = true)
    @PostMapping("/verifyELExpression")
    @ResponseBody
    public VerificationResult verifyELExpression(@Nullable @RequestBody CmpProperty jsonEl) {
        log.info("verifyELExpression 校验EL表达式");
        VerificationResult result = VerificationResult.builder().code("E").message("校验失败").build();
        try {
            result = bizService.verifyELExpression(jsonEl);
        } catch (Exception ex) {
            result.setCode("E");
            result.setMessage(ex.getMessage());
            log.error("-----", ex);
        }
        log.info("verifyELExpression 校验EL表达式完成");
        return result;
    }

    @ApiOperation(value = "新增LF链", notes = "构建一个Chain")
    @ApiImplicitParam(name = "createChainVO", value = "构建Chain入参", required = true)
    @PostMapping("/createChain")
    @ResponseBody
    public CommonResult createChain(@Nullable @RequestBody CreateChainVO createChainVO) {
        log.info("createChain 收到数据: {}", JsonUtil.toJsonString(createChainVO));
        CommonResult result = new CommonResult();
        try {
            result = bizService.createChain(createChainVO);
        } catch (Exception ex) {
            result.setCode("E");
            result.setMessage(ex.getMessage());
            log.error("-----", ex);
        }
        log.info("createChain 完成");
        return result;
    }

    @ApiOperation(value = "删除LF链", notes = "销毁一个Chain")
    @ApiImplicitParam(name = "deleteChainVO", value = "销毁Chain入参", required = true)
    @PostMapping("/deleteChain")
    @ResponseBody
    public CommonResult deleteChain(@Nullable @RequestBody DeleteChainVO deleteChainVO) {
        log.info("deleteChain 销毁Chain");
        CommonResult result = new CommonResult();
        try {
            result = bizService.deleteChain(deleteChainVO);
        } catch (Exception ex) {
            result.setCode("E");
            result.setMessage(ex.getMessage());
            log.error("-----", ex);
        }
        log.info("deleteChain 完成");
        return result;
    }

    @ApiOperation(value = "修改LF链", notes = "更新一个Chain")
    @ApiImplicitParam(name = "updateChainVO", value = "更新Chain入参", required = true)
    @PostMapping("/updateChain")
    @ResponseBody
    public CommonResult updateChain(@Nullable @RequestBody UpdateChainVO updateChainVO) {
        log.info("updateChain 更新链");
        CommonResult result = new CommonResult();
        try {
            result = bizService.updateChain(updateChainVO);
        } catch (Exception ex) {
            result.setCode("E");
            result.setMessage(ex.getMessage());
            log.error("-----", ex);
        }
        log.info("updateChain 完成");
        return result;
    }

    @ApiOperation(value = "获取LF链详情", notes = "获取一个Chain详情")
    @GetMapping("/getChainById")
    @ResponseBody
    public ChainInfo getChainById(String chainId) {
        log.info("getChainById 查询Chain，id: {}.", chainId);
        ChainInfo chainInfo = new ChainInfo();
        try {
            chainInfo = bizService.getChainById(chainId);
        } catch (Exception ex) {
            log.error("-----", ex);
        }
        log.info("getChainById 完成: {}", JsonUtil.toJsonString(chainInfo));
        return chainInfo;
    }

}
